<?php

namespace app\controller;

use app\model\user as UserModel;
use app\Request;
use app\validate\User as UserValidate;
use think\exception\ValidateException;
use think\facade\View;

class Upload
{
    public function index(){
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('image');
            // 上传到本地服务器
            $savename = \think\facade\Filesystem::putFile( 'topic', $file);
        return view('D:\php\phpEnv\www\www.tp.com\view\borrow.html', [
            'url'     => $savename
        ]);
    }

    public function add(Request $request)
    {
        $data = $request->param();
        //数据验证
//        try {
//            validate(UserValidate::class)->batch(true)->check($data);
//        } catch (ValidateException $exception) {
//            return view('view/public/toast.html',[
//                'infos'     =>  $exception->getError(),
//                'url_text'  =>  '返回上一页',
//                'url_path'  =>  url('/user/create')
//
//            ]);
//        }
        //写入数据库
        //新增后返回id
        $id = UserModel::create($data)->getData('id');
        //提示
        return $id ? view('./view/public/toast.html', [
            'infos' => ['恭喜，添加成功！'],
            'url_text' => '去首页',
            'url_path' => url('/')
        ]) : '添加失败！';
//        return 123;

    }


}